#!/bin/sh

if [ -z $1 ];then
	uname1=$(uname)
	if [ 'Linux' = $uname1 ];then
      day=$(date -d "1 days ago" +%Y%m%d)
    else
	  day=$(date -v -1d +%Y%m%d)
    fi
else
  day=$1
fi

part_day=`date -d "${day}" +%Y%m01`
CUR_DIR=$(cd "$(dirname "$0")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname $(dirname "$PWD"))))

source ${UP_DIR}/system.properties

filename=$2
opts=""
SQOOP_HOME="/usr/hdp/2.6.4.0-91/sqoop"
if [ -f "/opt/client/bigdata_env" ];then
    source /opt/client/bigdata_env
    opts="-D fs.s3a.access.key=S24JTQESPF5WIEOWZ4SK -D fs.s3a.secret.key=A8eOjlwNSNZNEvON1MxZQgLRNju4jdmYHddG2BZ3 -D fs.s3.endpoint=obs.cn-north-4.myhuaweicloud.com "
    SQOOP_HOME="/opt/sqoop/sqoop-1.4.7.bin__hadoop-2.6.0"
fi

suffix=`date -d "${day}" +%y`
partition_value=$day
start=$(date +%s)
files=`ls ${CUR_DIR}/day_week_month_files/*/*.config`
if [[ ${filename} != "" ]];then
	files=`ls ${CUR_DIR}/$filename`
fi
for f in `ls ${files}`
do
source ${UP_DIR}/system.properties
source ${f}
type=`echo ${f} | awk -F '/' '{print $(NF-1)}'`
if [[ "$type" == "week" ]];then
        whichday=`date -d $day +%w`
        partition_value=`date -d "${day} -$[${whichday}-1] days" +%Y%m%d`
fi
if [[ "$type" == "month" ]];then
        partition_value=`date -d "${day}" +%Y%m01`
fi
if [[ "$type" == "year" ]];then
        partition_value=`date -d "${day}" +%Y0101`
fi
__hive_partition_key=" "
__hive_partition_value=" "
__mysql_table_name="${mysql_table_name} "

if [[ "${hive_partitioned_table}" = 1 ]];then
    __hive_partition_key="--hive-partition-key ${hive_partition_key} "
    __hive_partition_value="--hive-partition-value ${partition_value} "
fi

if [[ "${mysql_support_suffix}" = 1 ]];then
   __mysql_table_name="${mysql_table_name}_${suffix}"
fi

echo "开始导入${day}号${__mysql_table_name}分区为 ${__hive_partition_value} 的数据"

echo "开始导入列 ${hive_columns} "

${SQOOP_HOME}/bin/sqoop export \
-D mapred.job.queue.name=${queue_name} -D sqoop.export.records.per.statement=1000 -D sqoop.export.statements.per.transaction=10 ${opts} \
--connect jdbc:mysql://${db_url}/${db_database}?useSSL=false \
--username ${db_username} \
--password ${db_pwd} \
--update-key ${mysql_update_key} \
--update-mode allowinsert \
--table ${__mysql_table_name} \
--fields-terminated-by '\t' \
--hcatalog-database ${hive_database} \
--hcatalog-table ${hive_table} ${__hive_partition_key} ${__hive_partition_value} \
--columns ${hive_columns}
    if [ $? -ne 0 ] ; then
        echo "${f}执行失败..."
    else
        echo "${f}执行成功..."
    fi
done
end=$(date +%s)
echo "ENDTIME：`date '+%Y%m%d-%H%M%S'`"
time=$(( $end - $start ))
echo "DURATION(S): $time"